﻿Public Class frmLopHoc

    Private Sub frmLopHoc_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LayDSLopHoc()
    End Sub

    Private Sub LayDSLopHoc()
        Try
            Dim lhBUS As New LopHocBUS
            Dim ds As New List(Of LopHocDTO)
            Dim i As Integer = 1

            ds = lhBUS.LayDanhSachLopHoc()
            dgLopHoc.Rows.Clear()

            For Each lh As LopHocDTO In ds
                dgLopHoc.Rows.Add(i, lh.MaLop, lh.TenLop, lh.Khoi)
                i += 1
            Next
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub btThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btThem.Click
        Try
            If (txtMaLop.Text.Trim.Length = 0) Then
                MessageBox.Show("Nhap Ma Lop.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Return
            ElseIf (txtTenLop.Text.Trim.Length = 0) Then
                MessageBox.Show("Nhap Ten Lop.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Return
            ElseIf (cbKhoi.SelectedItem.ToString().Trim().Length = 0) Then
                MessageBox.Show("Chon Khoi.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Return
            End If

            Dim lhDTO As New LopHocDTO()
            Dim lhBUS As New LopHocBUS()
            Dim kq As Integer = -1

            lhDTO.MaLop = txtMaLop.Text.Trim()
            lhDTO.TenLop = txtTenLop.Text.Trim()
            lhDTO.Khoi = cbKhoi.SelectedItem.ToString().Trim()

            kq = lhBUS.ThemLopHoc(lhDTO)

            If kq > 0 Then
                LayDSLopHoc()
            Else
                MessageBox.Show("Them that bai.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If

        Catch ex As Exception
            If (ex.Message.IndexOf("create duplicate values in the index, primary key, or relationship") > 0) Then
                MessageBox.Show("Ma Lop da ton tai.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Else
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        End Try
    End Sub

    Private Sub btXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btXoa.Click
        Try
            If (txtMaLop.Text.Trim.Length = 0) Then
                MessageBox.Show("Nhap Ma Lop muon Xoa.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Return
            End If

            Dim lhDTO As New LopHocDTO()
            Dim lhBUS As New LopHocBUS()
            Dim kq As Integer = -1

            lhDTO.MaLop = txtMaLop.Text.Trim()
            kq = lhBUS.XoaLopHoc(lhDTO)

            If kq > 0 Then
                LayDSLopHoc()
            Else
                MessageBox.Show("Xoa that bai.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub btCapNhat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btCapNhat.Click
        Try
            If (txtMaLop.Text.Trim.Length = 0) Then
                MessageBox.Show("Nhap Ma Lop.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Return
            ElseIf (txtTenLop.Text.Trim.Length = 0) Then
                MessageBox.Show("Nhap Ten Lop.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Return
            ElseIf (cbKhoi.SelectedItem.ToString().Trim().Length = 0) Then
                MessageBox.Show("Chon Khoi.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Return
            End If

            Dim lhDTO As New LopHocDTO()
            Dim lhBUS As New LopHocBUS()
            Dim kq As Integer = -1

            lhDTO.MaLop = txtMaLop.Text.Trim()
            lhDTO.TenLop = txtTenLop.Text.Trim()
            lhDTO.Khoi = cbKhoi.SelectedItem.ToString().Trim()

            kq = lhBUS.SuaLopHoc(lhDTO)

            If kq > 0 Then
                LayDSLopHoc()
            Else
                MessageBox.Show("Cap Nhat that bai.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub btTim_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btTim.Click
        Try
            Dim lhDTO As New LopHocDTO()
            Dim lhBUS As New LopHocBUS()
            Dim kq As Integer = -1

            lhDTO.MaLop = txtMaLop.Text.Trim()
            lhDTO.TenLop = txtTenLop.Text.Trim()
            lhDTO.Khoi = cbKhoi.SelectedItem.ToString().Trim()

            Dim ds As New List(Of LopHocDTO)
            Dim i As Integer = 1

            ds = lhBUS.TraCuuLopHoc(lhDTO)
            dgLopHoc.Rows.Clear()

            For Each lh As LopHocDTO In ds
                dgLopHoc.Rows.Add(i, lh.MaLop, lh.TenLop, lh.Khoi)
                i += 1
            Next

        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub btXoaForm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btXoaForm.Click
        txtMaLop.Text = ""
        txtTenLop.Text = ""
        cbKhoi.SelectedItem = ""
    End Sub

    Private Sub btThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btThoat.Click
        Me.Close()
    End Sub

    Private Sub dgLopHoc_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgLopHoc.SelectionChanged
        Try
            If (dgLopHoc.SelectedRows.Count > 0) Then
                If (dgLopHoc.SelectedRows(0).Cells(1).Value = Nothing) Then
                    Return
                End If

                txtMaLop.Text = dgLopHoc.SelectedRows(0).Cells(1).Value.ToString().Trim()
                txtTenLop.Text = dgLopHoc.SelectedRows(0).Cells(2).Value.ToString().Trim()
                cbKhoi.SelectedItem = dgLopHoc.SelectedRows(0).Cells(3).Value.ToString().Trim()
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub
    
End Class